package stream

import (
	"io/ioutil"
	. "mana/buyer"
	"os"
	"os/exec"
	"path/filepath"
)

type Streamer struct {
	StreamInter
}

func getFileList(path string) []string {
	list := []string{}
	dirlist, _ := ioutil.ReadDir(path)
	for _, v := range dirlist {

		if v.IsDir() == false {

			list = append(list, v.Name())

		}

	}
	return list

}

func getFilePath(path string) (abspath string) {
	file, _ := exec.LookPath(path)
	absfile, _ := filepath.Abs(file)
	abspath = filepath.Dir(absfile)
	return
}

func (*Streamer) Reader(path string) (string, []string) {
	abspath := getFilePath(path)
	list := getFileList(abspath)
	return abspath, list
}

func (*Streamer) Writer(namepath string, dat string) (int, error) {
	file, err := os.OpenFile(namepath, os.O_RDWR|os.O_EXCL, 0777)
	if os.IsNotExist(err) {
		file, err = os.OpenFile(namepath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0777)
	}
	defer file.Close()
	Check(err)

	return file.Write([]byte(dat))
}

func (*Streamer) ReadData(filepath string) string {
	Where()
	println(filepath)
	dat, err := ioutil.ReadFile(filepath)
	Check(err)
	return string(dat)
}

func (*Streamer) WriteData() {

}
